package com.briup.bms.service.impl;

import com.briup.bms.bean.SWarehouse;
import com.briup.bms.common.utils.BeanUtils.ColaBeanUtils;
import com.briup.bms.dao.IEmpMapper;
import com.briup.bms.dao.IRegionDao;
import com.briup.bms.dao.SWarehouseMapper;
import com.briup.bms.service.SWareHouseService;
import com.briup.bms.web.vm.WareHouseVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author: Paocha
 * @date: 2021-11-22-23:09
 */
@Service
public class WareHouseServiceImpl implements SWareHouseService {
    @Autowired
    SWarehouseMapper WarehouseMapper;
    @Autowired
    IRegionDao RegionDao;
    @Autowired
    IEmpMapper empMapper;

    @Override
    public PageInfo<WareHouseVO> findByCondition(int pageNum, int pageSize, Integer id, String region, String manager) {
        PageHelper.startPage(pageNum, pageSize);
        Integer regionId= Math.toIntExact(RegionDao.findByName(region).getId());
        Integer managerId=empMapper.findUidByName(manager);
        List<SWarehouse> sWarehouses = WarehouseMapper.selectByCondition(id,regionId,managerId);
        List<WareHouseVO> wareHouseVOS = ColaBeanUtils.copyListProperties(sWarehouses,WareHouseVO::new,(sWarehouse,wareHouseVO)->{
            wareHouseVO.setRegion(RegionDao.findById(Long.valueOf(sWarehouse.getRegionId())).getName());
            wareHouseVO.setManager(empMapper.findUserId(String.valueOf(sWarehouse.getRegionId())).getUsername());
        });
        return new PageInfo<>(wareHouseVOS);
    }

    @Override
    public PageInfo<WareHouseVO> findByPageHelper(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<SWarehouse> sWarehouses =WarehouseMapper.selectAll();
        List<WareHouseVO> wareHouseVOS = ColaBeanUtils.copyListProperties(sWarehouses,WareHouseVO::new,(sWarehouse,wareHouseVO)->{
            String region=null;
            String manager=null;
            region=RegionDao.findById(Long.valueOf(sWarehouse.getRegionId())).getName();
            manager=empMapper.findUserId(String.valueOf(sWarehouse.getRegionId())).getUsername();
            wareHouseVO.setRegion(region);
            wareHouseVO.setManager(manager);
        });

        return new PageInfo<>(wareHouseVOS);
    }

    @Override
    public void insert(WareHouseVO wareHouseVO) {
        SWarehouse sWarehouse=new SWarehouse();
        BeanUtils.copyProperties(wareHouseVO,sWarehouse);
        WarehouseMapper.insert(sWarehouse);
    }

    @Override
    public void updateWareHouse(WareHouseVO wareHouseVO) {
        SWarehouse sWarehouse=new SWarehouse();
        BeanUtils.copyProperties(wareHouseVO,sWarehouse);
        WarehouseMapper.updateByPrimaryKey(sWarehouse);
    }

    @Override
    public void batchDelete(Integer[] ids) {
        WarehouseMapper.batchDelete(ids);
    }
}
